package com.example.rabbitmq.controller;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @Date 2021/7/19 16:22
 * @Created by wangle
 * @Description
 */
@Slf4j
@RestController
@RequestMapping("/ttl")
public class SendMessage {
    @Autowired
    AmqpTemplate amqpTemplate;

    @Autowired
    private  RabbitTemplate rabbitTemplate;
    //开始发消息
    @GetMapping("/sendMsg/{msg}")
    public  void  SendMsg(@PathVariable String msg ){
        log.info("当前时间{} 发送一条消息给ttl队列{}",new Date().toString(),msg);
       amqpTemplate.convertAndSend("X","XA","消息来自ttl为10秒的队列："+msg);


    }

    //开始发消息 ttl
    @GetMapping("/sendExpirationMsg/{msg}/{ttl}")
    public  void  SendMsg2(@PathVariable String msg,
                           @PathVariable String ttl){
        log.info("当前时间{} 发送一条时长{}毫秒的消息给ttl队列QC{}",new Date().toString(),ttl,msg);
        rabbitTemplate.convertAndSend("X","XC","消息来自ttl为"+ttl+"毫秒的队列："+msg,msgt->{
        //发送消息的时候，延迟时长
            msgt.getMessageProperties().setExpiration(ttl);
            return msgt;
        });


    }


}
